//
//  BaseNavigationController.m
//  WXMovie
//
//  Created by apple on 15/8/26.
//  Copyright (c) 2015年 Mr.Y. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UINavigationControllerDelegate>

@property (nonatomic , weak) id popDelegate;

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
    //Navi 样式
   self.navigationBar.hidden = YES;
//    UIView *view = [UIView new];
//    view.frame = CGRectMake(0, 0, SCREENWIDTH, TopBarHeight);
//    view.backgroundColor = TotalWhite;
//    [self.view addSubview:view];
    UINavigationBar *appearance = [UINavigationBar appearance];
    [appearance setTranslucent:YES];
    //导航栏颜色
    UIColor * navColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"header_Navi"]];
    [appearance setBarTintColor:navColor];
    //item和按钮颜色，不是title的颜色
    [appearance setTintColor:[UIColor redColor]];
    [appearance setBackgroundColor:[UIColor whiteColor]];
//    [appearance setBackgroundImage:[UIImage imageNamed:@"nav-bg_14"] forBarMetrics:UIBarMetricsDefault];
    //设置导航栏文字颜色
    [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}];
    
    //设置所有返回按钮样式
    UIBarButtonItem *defaultBtn = [UIBarButtonItem appearance];
    UIImage *backImage = [[UIImage imageNamed:@"back"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [defaultBtn setBackgroundImage:backImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [defaultBtn setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
}

/*
 iOS7.0复写方法，设置状态栏的风格
 多级控制器时，应该在导航控制器中管理状态栏的风格
 */

//覆写状态栏风格方法
- (UIStatusBarStyle)preferredStatusBarStyle {

    return UIStatusBarStyleLightContent;
}

#pragma mark ----
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super pushViewController:viewController animated:animated];
    
    if (self.viewControllers.count > 0) {
        //支持手势返回
        self.interactivePopGestureRecognizer.delegate = nil;
        if (viewController == [self.viewControllers firstObject])
        {
            
        } else
        {
            //创建导航栏左边按钮
            UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            if (@available(iOS 11.0, *))
            {
                leftBtn.frame = CGRectMake(0, TopStatusHeight_xs, 25, 25);
            } else {
                // Fallback on earlier versions
                leftBtn.frame = CGRectMake(0, TopStatusHeight, 25, 25);
            }
            [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
            [leftBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
            viewController.navigationItem.leftBarButtonItem = leftItem;
        }
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == [self.viewControllers firstObject]) {
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }
}


#pragma mark - Actions
//返回按钮
- (void)backBtnAction
{
    [self popViewControllerAnimated:YES];
}

@end
